package com.hit.sys.modular.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.core.enums.CommonStatusEnum;
import com.hit.core.enums.YesOrNotEnum;
import com.hit.core.exception.ServiceException;
import com.hit.core.factory.PageFactory;
import com.hit.core.pojo.page.PageResult;
import com.hit.sys.modular.config.entity.SysConfig;
import com.hit.sys.modular.config.enums.SysConfigExceptionEnum;
import com.hit.sys.modular.config.mapper.SysConfigMapper;
import com.hit.sys.modular.config.param.SysConfigParam;
import com.hit.sys.modular.config.service.SysConfigService;
import org.springframework.stereotype.Service;

@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Override
    public PageResult<SysConfig> page(SysConfigParam sysConfigParam) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(sysConfigParam)){
            if(ObjectUtil.isNotEmpty(sysConfigParam.getCode())){
                queryWrapper.like(SysConfig::getCode, sysConfigParam.getCode());
            }
            if(ObjectUtil.isNotEmpty(sysConfigParam.getName())){
                queryWrapper.like(SysConfig::getName, sysConfigParam.getName());
            }
            if(ObjectUtil.isNotEmpty(sysConfigParam.getGroupCode())){
                queryWrapper.eq(SysConfig::getGroupCode, sysConfigParam.getGroupCode());
            }
        }

        queryWrapper.ne(SysConfig::getStatus, CommonStatusEnum.DELETED.getCode());
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public void add(SysConfigParam sysConfigParam) {
        checkParam(sysConfigParam, false);

        SysConfig sysConfig = new SysConfig();
        BeanUtil.copyProperties(sysConfigParam, sysConfig);
        sysConfig.setStatus(CommonStatusEnum.ENABLE.getCode());

        this.save(sysConfig);
    }

    @Override
    public void edit(SysConfigParam sysConfigParam) {
        checkParam(sysConfigParam, true);
        SysConfig sysConfig = this.getEntity(sysConfigParam);
        BeanUtil.copyProperties(sysConfigParam, sysConfig);

        sysConfig.setStatus(null);
        this.updateById(sysConfig);
    }

    @Override
    public void delete(SysConfigParam sysConfigParam) {
        SysConfig sysConfig = this.getEntity(sysConfigParam);
        if (YesOrNotEnum.Y.getCode().equals(sysConfig.getSysFlag())) {
            throw new ServiceException(SysConfigExceptionEnum.CONFIG_SYS_CAN_NOT_DELETE);
        }

        sysConfig.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(sysConfig);
    }

    @Override
    public SysConfig detail(SysConfigParam sysConfigParam) {
        return this.getEntity(sysConfigParam);
    }

    private void checkParam(SysConfigParam sysConfigParam, boolean isExcludeSelf) {
        Long id = sysConfigParam.getId();
        String name = sysConfigParam.getName();
        String code = sysConfigParam.getCode();

        //构建带name和code的查询条件
        LambdaQueryWrapper<SysConfig> queryWrapperByName = new LambdaQueryWrapper<>();
        queryWrapperByName.eq(SysConfig::getName, name);

        LambdaQueryWrapper<SysConfig> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(SysConfig::getCode, code);

        //如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByName.ne(SysConfig::getId, id);
            queryWrapperByCode.ne(SysConfig::getId, id);
        }
        int countByName = this.count(queryWrapperByName);
        int countByCode = this.count(queryWrapperByCode);

        //如果存在重复的记录，抛出异常，直接返回前端
        if (countByName >= 1) {
            throw new ServiceException(SysConfigExceptionEnum.CONFIG_NAME_REPEAT);
        }
        if (countByCode >= 1) {
            throw new ServiceException(SysConfigExceptionEnum.CONFIG_CODE_REPEAT);
        }
    }

    private SysConfig getEntity(SysConfigParam sysConfigParam){
        SysConfig entity = this.getById(sysConfigParam.getId());
        if(ObjectUtil.isEmpty(entity)){
            throw new ServiceException(SysConfigExceptionEnum.CONFIG_NOT_EXIST);
        }
        return entity;
    }
}
